package com.rlz.framemm.config

import android.annotation.SuppressLint
import android.content.Context
import com.bumptech.glide.Glide
import com.bumptech.glide.GlideBuilder
import com.rlz.image.GlideAppliesOptions
import com.rlz.image.config.ImageConfig
import com.rlz.image.imageloader.BaseImageLoaderStrategy

/**
 * Created by RLZ
 * on 2021/6/16
 *
 * Glide 加载方式 可以扩展其他加载
 */
class ImageLoaderStrategy : BaseImageLoaderStrategy<Image>, GlideAppliesOptions {

    @SuppressLint("VisibleForTests")
    override fun applyGlideOptions(context: Context, builder: GlideBuilder) {
        Glide.init(context, builder)
    }

    override fun <T : ImageConfig> loadImage(ctx: Context, config: T?) {
        val manager = Glide.with(ctx)
        manager.load(config!!.url)
            .placeholder(config.placeholder)
            .error(config.errorId)
            .into(config.imageView!!)
    }

    override fun <T : ImageConfig> clear(ctx: Context, config: T?) {
        val manager = Glide.with(ctx)
        manager.clear(config!!.imageView!!)
    }
}